package com.kstech.exccheck.receiver;

import android.app.Application;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

import com.kstech.exccheck.domain.checkline.CheckLineManager;
import com.kstech.exccheck.exception.ExcException;
import com.kstech.exccheck.util.AppContext;
import com.kstech.exccheck.util.DateUtil;
import com.kstech.exccheck.util.DialogUtil;
import com.kstech.exccheck.view.upload.Globals;

import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class WifiService extends Service {
    private WifiService service;
    private Timer timer;
    private WifiManager wifiManager;
    private int mWIFIstate;
    private int mLastResult;
    private  int resultLv;
    private ConnectivityManager connmanager;
    private NetworkInfo networkInfo;

    public WifiService() {
        service = this;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
                connmanager = (ConnectivityManager)getApplicationContext().getSystemService(
                        Context.CONNECTIVITY_SERVICE);
                networkInfo = connmanager.getActiveNetworkInfo();
                if(Globals.WIFISTATUS.size() > 25){
                    try {
                        DialogUtil.showDialog(AppContext.context,"当前检线信号弱，正在重新连接",false);
                    }catch (Exception e){
                        Log.e("SERVICE_WIFI","service DialogUtil -- "+e);
                    }
                    CheckLineManager.connectCheckLine(AppContext.getCurrentCheckLine());
                    Globals.WIFISTATUS.clear();
                }
                if(networkInfo ==null || networkInfo.getState() == NetworkInfo.State.DISCONNECTED){
                    Log.e("SERVICE_WIFI","service network info -- "+networkInfo);
                    Globals.WIFISTATUS.add(0);
                }
                mWIFIstate = wifiManager.getWifiState();

                if(mWIFIstate == WifiManager.WIFI_STATE_ENABLED){
                    WifiInfo info = wifiManager.getConnectionInfo();

                    List<ScanResult> results = wifiManager.getScanResults();
                    for (ScanResult result : results) {

                        if(info.getSSID().equals("\""+result.SSID+"\"")){
                            resultLv = WifiManager.calculateSignalLevel(result.level, 100);

                            if(mLastResult != resultLv){
                                Globals.WIFISTATUS.clear();
                            }
                        }
                    }
                }else{
                    if(mWIFIstate == WifiManager.WIFI_STATE_DISABLED){

                        if(mLastResult != resultLv){
                            Globals.WIFISTATUS.add(0);
                        }
                    }
                }
                mLastResult = resultLv;
            }
        },1000,1000);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return new MyBinder();
    }


    @Override
    public void onDestroy() {
        timer.cancel();
    }


    public void stopTimer(){
        timer.cancel();
    }

    public class MyBinder extends Binder{
        public WifiService getService(){
            return service;
        }
    }
}
